#include "oom.h"

void oom(){
  struct linked_list* l = NULL;
  struct linked_list* current, *tmp;
  char buf[10];
  int pid, i = 0;
  int my_prio = obtenir_priorite(obtenir_pid());
  int child_prio = (my_prio == MAX_PRIORITY?my_prio:my_prio+1);
  ecrire("Process creation started\n");
  while(1){
    pid = fourchette(child_prio, "some_task",NULL);
    if(pid == OUT_OF_MEMORY)
      break;
    //let's keep the pid in the list (to be able to kill it later)
    current = allouer(sizeof(struct linked_list));
    if(current == NULL)
      // out of memory
      break;
    current->pid = pid;
    current->next = l;
    l = current;
    i ++;
    retarder(obtenir_pid(),100);
  }
  ecrire("There we are: OUT OF MEMORY ! \\o/\n");
  ecrire(itoa(i, buf, 10));
  ecrire(" processes created !\nNow, let's clean the place.\n");
  current = l;
  i = 0;
  while(current != NULL){
    tmp = current->next;
    if(tuer(current->pid, 0) == SUCCESS && liberer(current) == SUCCESS)
      i ++;
    current = tmp;
  }
  ecrire(itoa(i, buf, 10));
  ecrire(" processes freed !\n\n");
  //quitter(1);
}

void some_task(){
  ecrire("Process started\n");
  while(1);
}
